/* Copyright (C) 2024 wlly-lzh
 * Created On 24/03/12
 * Singly Linked List  单链表(未封装)
 * */
#ifndef _SLLIST_
#define _SLLIST_

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct Singly_Linked_List Singly_Linked_List;
typedef struct Singly_Linked_List SLList;
struct Singly_Linked_List{
	void *Data;
	struct Singly_Linked_List * Next;
};
bool Destroy_SLList(SLList *List,void (*FreeData)(void *)){
	if(List != NULL){
		SLList * swap=List;
		while(List != NULL){
			List=List->Next;
			(*FreeData)(swap->Data);
			free(swap);
			swap=List;
		}
		return true;
	}else{
		printf("The Singly Linked List is NULL!\n");
		return false;
	}
}

// addr mast > 1
// 1 2 3 4 5 6 7
bool Insert_SLList(SLList *List,unsigned int addr,void *Data){
	for(unsigned int i=1;i<addr-1;++i){
		List=List->Next;
	}
	SLList *newList=(SLList *)malloc(sizeof(SLList));
	if(newList ==NULL){
		printf("(Error) Malloc False!\n");
		exit(3);
	}
	newList->Data=Data;
	newList->Next=List->Next;
	List->Next=newList;
	return true;
}
bool Delete_SLList(SLList *List,unsigned int addr,
		void (*FreeData)(void *)){
	if(List != NULL){
		for(unsigned int i=1; i<addr; ++i){
			List=List->Next;
		}
		SLList *Del=List->Next;
		List->Next=List->Next->Next;
		FreeData(Del->Data);
		free(Del);
		return true;
	}
	exit(4);
}
// 获取节点，而不是data
bool GetNode_SLList(SLList *List, unsigned int addr,SLList **Note){
	if(List == NULL){
		exit(4);
	}
	if(addr > 0 && List != NULL){
		for(unsigned int i=1; i<addr ;++i){
			List=List->Next;
		}
		*Note=List;
		return true;
	}else
		return false;
}
// 1 2 3 4 5 6
void Update_SLList(SLList *List,unsigned int addr,void *Data,
		void (*FreeData)(void *)){
	for(unsigned int i=1; i<addr; ++i){
		List=List->Next;
	}
	(*FreeData)(List->Data);
	List->Data=Data;
}
#endif
